package com.welink.web.common.util;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import java.io.UnsupportedEncodingException;

public class ParameterUtil {

    private static org.slf4j.Logger log = LoggerFactory.getLogger(ParameterUtil.class);

    public static String getParameter(String parameterName) throws UnsupportedEncodingException {

        ServletRequest request = ServletActionContext.getRequest();
        String result = request.getParameter(parameterName);
//        if(org.apache.commons.lang3.StringUtils.isNotBlank(result)){
//            //result = new String(result.getBytes(), "UTF-8");//"ISO-8859-1"
//            return result;
//        }
        //result = EmojiFilter.filterEmoji(result);
        if (StringUtils.isNotBlank(result)) {
            return result;
        }

        return "";
    }

    public static long getParameterAslong(String parameterName) {

        ServletRequest request = ServletActionContext.getRequest();

        String result = request.getParameter(parameterName);

        long l = -1l;


        if (StringUtils.isNotBlank(result)) {
            try {
                return Long.valueOf(result);
            } catch (Exception e) {
                log.error("get long value paramteters failed . exp:" + e.getMessage());
            }
        }

        return -1l;
    }

    public static int getParameterAsInt(String parameterName) {

        ServletRequest request = ServletActionContext.getRequest();

        String result = request.getParameter(parameterName);

        int l = -1;


        if (StringUtils.isNotBlank(result)) {
            try {
                return Integer.valueOf(result);
            } catch (Exception e) {
                log.error("get integer value paramteters failed . exp:" + e.getMessage());
            }
        }

        return -1;
    }

    public static byte getParameterAsByte(String parameterName) {

        ServletRequest request = ServletActionContext.getRequest();

        String result = request.getParameter(parameterName);

        byte l = -1;


        if (StringUtils.isNotBlank(result)) {
            try {
                return Byte.valueOf(result);
            } catch (Exception e) {
                log.error("get integer value paramteters failed . exp:" + e.getMessage());
            }
        }

        return (byte) -1;
    }


    public static String getUserIpAddress() {

        ServletRequest request = ServletActionContext.getRequest();

        String result = request.getRemoteAddr();

        if (StringUtils.isNotBlank(result)) {
            return result;
        }
        return "unknow";
    }


}
